package com.sdp.irrigation.db.irrigation_pump_well.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.db.IViewQuery;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.irrigation.db.irrigation_eq_valve.service.IIrrigationEqValveService;
import com.sdp.irrigation.db.irrigation_pump_well.mapper.IrrigationPumpWellMapper;
import com.sdp.irrigation.db.irrigation_pump_well.service.IIrrigationPumpWellService;
import com.sdp.irrigation.entity.IrrigationPumpWell;

/**
 * <p>
 * 机井台账信息 服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-12-14
 */
@Service
public class IrrigationPumpWellServiceImpl extends ServiceImpl<IrrigationPumpWellMapper, IrrigationPumpWell> implements IIrrigationPumpWellService ,IViewQuery{

	@Autowired
	private IrrigationPumpWellMapper irrigationPumpWellMapper;
	
	@Autowired
	private IIrrigationEqValveService iIrrigationEqValveService;
	
	private final Object lock = new Object();
	
	@Override
	public <T> List<T> viewList(QueryWrapper<T> queryWrapper,DataCenter dc) {
		return irrigationPumpWellMapper.viewList(queryWrapper,dc);
	}

	@Override
	public <T> int viewCount(QueryWrapper<T> queryWrapper) {
		return irrigationPumpWellMapper.viewCount(queryWrapper);
	}

	@Override
	public <T> IPage<T> viewPage(IPage<T> page, QueryWrapper<T> queryWrapper,DataCenter dc) {
		return irrigationPumpWellMapper.viewPage(page, queryWrapper,dc);
	}

	@Override
	public List<IrrigationPumpWell> queryAllPumpWell() {
		LambdaQueryWrapper<IrrigationPumpWell> lambdaQueryWrapper = new LambdaQueryWrapper<>();
		lambdaQueryWrapper.orderByDesc(IrrigationPumpWell::getCreateTime);
		return list(lambdaQueryWrapper);
	}

	@Override
	public void updatePumpWell(IrrigationPumpWell oldPumpWell, IrrigationPumpWell newPumpWell) {
		
		String oldEqNo = oldPumpWell.getEqNo();
		if(StringUtils.isEmpty(oldEqNo)) {
			oldEqNo = "";
		}
		
		//如果原来的设备与本次更新的设备编号不同,说明有更换设备
		if(!oldPumpWell.getEqNo().equals(newPumpWell.getEqNo())) {
			
			synchronized (lock) {
				//看新的设备是否有其他机井在用
				if (!StringUtils.isEmpty(newPumpWell.getEqNo())) {
					cleanPumpAndValveByEqNo(newPumpWell.getEqNo());
				}
				//删除老的设备对应的阀门信息
				if (!StringUtils.isEmpty(oldEqNo)) {
					//iIrrigationEqValveService.queryEqValveListByEqNo(oldPumpWell.getEqNo());
					iIrrigationEqValveService.removeValveMapByEqNo(oldPumpWell.getEqNo());
				}
			}
			
			
			
			
			
			//获取原来设备编号，将原来设备更新为空闲
//			iIrrigationEqService.updateEqUseStateByEqNo(oldPumpWell.getEqNo(), IrrigationConstants.设备使用状态_空闲.getCode());
			
			//将新设置的设备状态改为使用
//			iIrrigationEqService.updateEqUseStateByEqNo(newPumpWell.getEqNo(), IrrigationConstants.设备使用状态_使用.getCode());
			
			//更新设备实时信息表
//			IrrigationEqRealTime eqRealTime = iIrrigationEqRealTimeService.updateEqRealTime(oldPumpWell.getEqNo(), newPumpWell);
			
			//更新设备实时缓存
//			eqRealTimeCache.updateEqRealTimeCache(oldPumpWell.getEqNo(), eqRealTime);
			
			//更新阀门实时信息
//			Map<String,IrrigationEqValve> eqValveMap = iIrrigationEqValveService.updateEqValve(oldPumpWell.getEqNo(), newPumpWell.getEqNo());
			
			//更新阀门实时缓存
//			eqValveCache.updateEqValveCache(oldPumpWell.getEqNo(), newPumpWell.getEqNo(), eqValveMap);
			
		}
		
		newPumpWell.setUpdateTime(LocalDateTime.now());
		updateById(newPumpWell);
		
	}

	@Override
	public void addNewPumpWell(IrrigationPumpWell newPumpWell) {
		
		synchronized (lock) {
			//查询此设备是否已经用到其他机井,如果是，则清理
			cleanPumpAndValveByEqNo(newPumpWell.getEqNo());
			
			newPumpWell.setCreateTime(LocalDateTime.now());
			newPumpWell.setUpdateTime(LocalDateTime.now());
			save(newPumpWell);
		}
		
		
		
		//将新设置的设备状态改为使用
//		iIrrigationEqService.updateEqUseStateByEqNo(newPumpWell.getEqNo(), IrrigationConstants.设备使用状态_使用.getCode());
	
		//设置设备实时状态信息和缓存
//		IrrigationEqRealTime eqRealTime = iIrrigationEqRealTimeService.updateEqRealTime(newPumpWell.getEqNo(), newPumpWell);
//		eqRealTimeCache.updateEqRealTimeCache(newPumpWell.getEqNo(), eqRealTime);
		
		//更新阀门实时信息和缓存
//		Map<String,IrrigationEqValve> eqValveMap = iIrrigationEqValveService.updateEqValve(newPumpWell.getEqNo(), newPumpWell.getEqNo());
//		eqValveCache.updateEqValveCache(newPumpWell.getEqNo(), newPumpWell.getEqNo(), eqValveMap);
	}

	@Override
	public void updateCoverimg(String imgPath, String eqNo) {
		LambdaUpdateWrapper<IrrigationPumpWell> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.set(IrrigationPumpWell::getEqCoverimg, imgPath);
		lambdaUpdateWrapper.eq(IrrigationPumpWell::getEqNo, eqNo);
		update(lambdaUpdateWrapper);
	}

	@Override
	public void updateEqRepairStatusByEqNo(String eqNo, String eqRepairState) {
		LambdaUpdateWrapper<IrrigationPumpWell> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.set(IrrigationPumpWell::getEqRepairStatus, eqRepairState);
		lambdaUpdateWrapper.eq(IrrigationPumpWell::getEqNo, eqNo);
		update(lambdaUpdateWrapper);
		
	}

	@Override
	public IrrigationPumpWell queryPumpWellByEqNo(String eqNo) {
		LambdaUpdateWrapper<IrrigationPumpWell> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
		lambdaUpdateWrapper.eq(IrrigationPumpWell::getEqNo, eqNo);
		return getOne(lambdaUpdateWrapper,false);
	}

	/**
	 * 
	 * @param eqNo
	 */
	private void cleanPumpAndValveByEqNo(String eqNo) {
		//查询设备是否已经用到其他机井
		IrrigationPumpWell dbPumpWell = queryPumpWellByEqNo(eqNo);
		if(!StringUtils.isEmpty(dbPumpWell)) {
			//原来机井设备编号为空
			//dbPumpWell.setEqNo(null);
			UpdateWrapper<IrrigationPumpWell> updateWrapper = new UpdateWrapper<>();
			updateWrapper.lambda().eq(IrrigationPumpWell::getKeyId, dbPumpWell.getKeyId())
				.set(IrrigationPumpWell::getEqNo, null);
			update(updateWrapper);
			
//			updateById(dbPumpWell);
//			save(dbPumpWell);
		
			//删除此设备对应阀门信息
			iIrrigationEqValveService.removeValveMapByEqNo(eqNo);
			//iIrrigationEqValveService.queryEqValveListByEqNo(eqNo);
			
		}
	}
	
}
